
@SuppressWarnings("unchecked")
public class p1009_313ms_5th {

	public static//
	// -----
	// -----
	class Main {
		public static void main(String[] args) throws Exception {
			java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
			while (true) {
				width = Integer.parseInt(reader.readLine());
				System.out.println(width);
				if (width == 0)
					break;
				init();
				while (true) {
					String[] parts = reader.readLine().split(" ");
					int num = Integer.parseInt(parts[0]);
					int count = Integer.parseInt(parts[1]);
					if (count == 0) {
						finish();
						break;
					}
					put(num, count);
				}
			}
		}

		static int width;
		static java.util.List<int[]> prev, curr, next;
		static int nextSize, outVal, outCnt;

		static void init() {
			prev = curr = null;
			next = new java.util.ArrayList<int[]>();
			nextSize = outCnt = 0;
		}

		static void put(int num, int count) {
			if (count < width - nextSize) { // fast add
				nextSize += count;
				next.add(new int[] { num, nextSize });
				return;
			}
			if (nextSize > 0) { // finish a line
				next.add(new int[] { num, width });
				count -= width - nextSize;
				nextSize = 0;
				calcAndMove();
			}

			// don't use count >= width*3, for overflow issue
			if (count / width >= 3) { // has 0-zone
				next.add(new int[] { num, width }); // line 1
				count -= width;
				calcAndMove();
				next = curr; // line 2
				count -= width;
				calcAndMove();

				int size = count - width - count % width;
				count -= size;
				print(0, size);

				if (count > 0)
					put(num, count);
			} else {
				while (count >= width) {
					count -= width;
					next.add(new int[] { num, width });
					calcAndMove();
				}
				if (count > 0)
					put(num, count);
			}
		}

		static void calcAndMove() {
			if (curr != null) {
				java.util.List<Integer> gaps = new java.util.ArrayList<Integer>();
				for (java.util.List<int[]> list : java.util.Arrays.asList(prev, curr, next)) {
					if (list == null)
						continue;
					for (int i = 0; i < list.size() - 1; i++) {
						int gap = list.get(i)[1];
						gaps.add(gap - 1);
						gaps.add(gap + 1);
					}
				}
				java.util.Collections.sort(gaps);
				gaps.add(width);

				int cur = 0;
				for (int gap : gaps) {
					if (gap == cur)
						continue;
					int val;
					for (int i = 0; true; i++) {
						int[] entry = curr.get(i);
						if (entry[1] >= gap) {
							val = entry[0];
							break;
						}
					}
					java.util.List<Integer> surr = new java.util.ArrayList<Integer>();
					for (java.util.List<int[]> list : java.util.Arrays.asList(prev, curr, next)) {
						if (list == null)
							continue;
						for (int i = 0; i < list.size(); i++) {
							int[] entry = list.get(i);
							if (entry[1] < gap - 1)
								continue;
							surr.add(entry[0]);
							if (entry[1] >= gap + 1)
								break;
						}
					}
					print(diff(val, surr), gap - cur);
					cur = gap;
				}
			}
			prev = curr;
			curr = next;
			next = new java.util.ArrayList<int[]>();
		}

		static void finish() {
			next = null;
			calcAndMove();
			if (outCnt > 0)
				System.out.println(outVal + " " + outCnt);
			System.out.println("0 0");
		}

		static int diff(int cur, java.util.List<Integer> vals) {
			int max = 0;
			for (int v : vals)
				max = Math.max(max, Math.abs(cur - v));
			return max;
		}

		static void print(int val, int count) {
			if (outVal == val) {
				outCnt += count;
			} else {
				if (outCnt > 0) {
					System.out.println(outVal + " " + outCnt);
				}
				outVal = val;
				outCnt = count;
			}
		}
	}

	// -----
	// -----

	public static void main(String[] args) throws Exception {
		Judge.run();
	}

}
